#include "ResourceManager.h"
#include "DirectXMesh.h"
#include "Engine.h"
#include "GameState.h"

//
//std::map<std::string,LPDIRECT3DTEXTURE9>::iterator texIt = m_textures.begin();
//while(texIt != m_textures.end())
//{
//    texIt->second->Release();
//    texIt++;
//}
//m_textures.clear();
//
//std::map<std::string,DirectXMesh*>::iterator meshIt = m_meshes.begin();
//while(meshIt != m_meshes.end())
//{
//    meshIt->second->CleanUp();
//    delete meshIt->second;
//    meshIt++;
//}
//m_meshes.clear();

void ResourceManager::FlushMeshes()
{
    std::map<std::string,DirectXMesh*>::iterator it;
    it = m_meshes.begin();
    while(it != m_meshes.end())
    {
        it->second->CleanUp();
        delete it->second;
        it = m_meshes.erase(it);
    }
}
void ResourceManager::FlushTextures()
{
    std::map<std::string,Texture>::iterator it;
    it = m_textures.begin();
    while(it!= m_textures.end())
    {
        it->second->Release();
        it = m_textures.erase(it);
    }
    m_defaultTexture = NULL;
}
void ResourceManager::FlushTexture( const char* id )
{
    std::map<std::string,Texture>::iterator it;
    it = m_textures.find(std::string(id));
    if(it!= m_textures.end())
    {
        it->second->Release();
        m_textures.erase(it);
    }
}
void ResourceManager::FlushMesh( const char* id )
{
    std::map<std::string,DirectXMesh*>::iterator it;
    it = m_meshes.find(std::string(id));
    if(it!= m_meshes.end())
    {
        it->second->CleanUp();
        delete it->second;
        m_meshes.erase(it);
    }
}
Texture ResourceManager::GetTexture(const char* which )
{
    std::map<std::string,Texture>::iterator it;
    it = m_textures.find(std::string(which));
    if(it!= m_textures.end())
        return it->second;

    if(m_defaultTexture)
        return m_defaultTexture;

    std::string output = "Texture not found...no default set! : ";
    output += which;
    Engine::Instance()->GetCurrentState()->HandleMessage(output.c_str());
    assert(0);
    return NULL;
}
void ResourceManager::SetDefaultTexture(const char* ident)
{
    m_defaultTexture = GetTexture(ident);
}
void ResourceManager::LoadTexture( const char* path, const char* name )
{
    std::string toLoad(path);
    toLoad += std::string(name);
    std::map<std::string,Texture>::iterator it;
    it = m_textures.find(toLoad);
    if(it == m_textures.end())
    {
        LPDIRECT3DTEXTURE9 temp;
        TEST(D3DXCreateTextureFromFileA(Engine::Instance()->GetDevice(), toLoad.c_str(), &temp));
        m_textures[std::string(name)] = temp;
    }
    else
    {
        std::string output = "Texture name used twice! : ";
        output += toLoad;
        Engine::Instance()->GetCurrentState()->HandleMessage(output.c_str());
    }
}
void ResourceManager::LoadMesh( const char* path, const char* name)
{
    std::map<std::string,DirectXMesh*>::iterator it;
    it = m_meshes.find(std::string(name));
    if(it == m_meshes.end())
    {
        DirectXMesh* mesh = new DirectXMesh;
        mesh->MakeSharedMesh();
        mesh->SetTexture(GetTexture("default.png"));
        if(!mesh->CreateFromOBJ(path,name))
            assert(0);
        m_meshes[std::string(name)] = mesh;
    }
    else
    {
        assert(0);//mesh loaded twice
    }
}
DirectXMesh* ResourceManager::GetMesh( const char* which )
{
    std::map<std::string,DirectXMesh*>::iterator it;
    it = m_meshes.find(std::string(which));
    if(it!= m_meshes.end())
        return it->second;

    assert(0);
    return NULL;
}
void ResourceManager::AddMesh( const char* name, DirectXMesh* mesh )
{
    std::map<std::string,DirectXMesh*>::iterator it;
    it = m_meshes.find(std::string(name));
    if(it != m_meshes.end())
        delete mesh;
    else
        m_meshes[std::string(name)] = mesh;
}
